#!/bin/sh
#
# Source makefile
#
# $Id: Makefile 355 2012-03-21 15:19:45Z degtyarev.alexey $

CATEGORY=databases
NAME=mysqlbackup
TARBALL_SFX=tar.gz

REMOTE_USER=nice
REMOTE_HOST=nice.renatasystems.org
REMOTE_PATH=~/src

VERSION=${NAME}-`/usr/bin/head -1 VERSION`

FILES=mysqlbackup 200.mysqlbackup.*
DIRECTORIES=

EXEC_AFTER=

.SILENT:

all: clean tarball
tarball: clean
	echo "Creating distributive of ${NAME}: ${VERSION}.${TARBALL_SFX}"
	/bin/mkdir ${VERSION}
	/usr/bin/sed "s/VERSION/${VERSION}/g;" README > ${VERSION}/README
	/bin/cp VERSION RELNOTES TODO LICENSE ${VERSION}
	if [ -f Makefile.in ]; then\
		if [ -f ${VERSION}/Makefile ]; then\
			echo "conflict: ${VERSION}/Makefile already exist.";\
			exit 1;\
		fi;\
		/bin/cp Makefile.in ${VERSION}/Makefile;\
	fi
	if [ ! -z "${DIRECTORIES}" ]; then\
		/bin/cp -R ${DIRECTORIES} ${VERSION};\
	fi
	if [ ! -z "${FILES}" ]; then\
		/bin/cp ${FILES} ${VERSION};\
	fi
	if [ ! -z "${EXEC_AFTER}" ]; then\
		exec "${EXEC_AFTER}";\
	fi
	/usr/bin/sed -i "" -e "s/%%VERSION%%/`/usr/bin/head -1 VERSION`/g" \
		${VERSION}/mysqlbackup 
	/usr/local/bin/help2man \
		--version-option "-V" \
		--help-option "-H" \
		--no-info \
		--source=FreeBSD \
		--opt-include=mysqlbackup.1.include \
		--output=${VERSION}/mysqlbackup.1 \
		${VERSION}/mysqlbackup && \
	echo "==>  manpage mysqlbackup(1) created"
#	/usr/bin/nroff -mandoc <${VERSION}/mysqlbackup.1 |\
#	/usr/local/bin/man2html -seealso -pgsize 1500 \
#	-title "mysqlbackup - create everyday MySQL-databases backup" |\
#	/usr/local/bin/tidy -quiet -asxml -utf8 >${VERSION}/mysqlbackup.html && \
#	echo "==>  html page for mysqlbackup created"
	/usr/bin/tar zcf ${VERSION}.${TARBALL_SFX} ${VERSION}
	echo "==>  tarball for ${VERSION} created"
	tar tzf ${VERSION}.${TARBALL_SFX}

upload: tarball
	[ -f ${VERSION}.${TARBALL_SFX} ] || exit 1;
	/bin/cat ${VERSION}.${TARBALL_SFX} | /usr/bin/ssh ${REMOTE_USER}@${REMOTE_HOST} "\
		cd ${REMOTE_PATH};\
		if [ ! -d ${CATEGORY}/${NAME} ]; then\
			/bin/mkdir -p ${CATEGORY}/${NAME};\
		fi;\
		/bin/cat >${CATEGORY}/${NAME}/${VERSION}.${TARBALL_SFX}\
	"
	/bin/rm -r "${VERSION}"/* && /bin/rmdir ${VERSION} && /bin/rm ${VERSION}.${TARBALL_SFX}
	echo "Done"

clean:
	if [ -d ${VERSION} ]; then /bin/rm -r "${VERSION}"/* && /bin/rmdir ${VERSION}; fi
	if [ -f ${VERSION}.${TARBALL_SFX} ]; then /bin/rm ${VERSION}.${TARBALL_SFX}; fi
